import express, { Application, Request, Response, NextFunction } from 'express'
import tokenVerification from './middleware/tokenVerification'
import { Pool } from 'mysql'
import { port, header } from './config/setting'
const app: Application = express()
const createPool = require('./db')
const routes = require('./routes')
// 顶层声明 app 该代码不可更改
type APP = typeof app
declare global {
    namespace NodeJS {
        interface Global {
            appGlobal: APP
        }
    }
}
global.appGlobal = app

// cross origin
app.all('*', function (req: Request, res: Response, next: NextFunction) {
    for (const key in header) {
        res.header(key, (header as any)[key])
    }
  next()
})
// bodyParse
app.use(express.json({limit: '50mb'}))
app.use(express.urlencoded({limit: '50mb'}))
// token验证拦截中间件
app.use(tokenVerification)
// createPool 顶层声明数据库连接池 该代码不可更改
declare global {
  namespace NodeJS {
      interface Global {
          poolGlobal: Pool
      }
  }
}
global.poolGlobal = createPool()
/* routes */
routes()
app.listen(port, function(){
    console.log(`app serve listening on port ${port}!`);
})